<?php

require_once($_SERVER['DOCUMENT_ROOT'].'/includes/site_defaults.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/database/db.php');

if (!isset($_SESSION)) {
	session_start();
}
//
$contact_id = $_SESSION['id'];
$dealer_name = "";
$phone = "";
$address1 = "";
$address2 = "";
$city = "";
$state = "";
$zip = "";
$description = "";
$dealership_id = -1;
 
if (isset($_GET['id']) && $_GET['id'] > 0) {
	// Editing an existing dealer
	$qry = "SELECT * FROM dealerships WHERE id = ".$_GET['id'];
	mysql_select_db($db_name, $db);
	$rs = mysql_query($qry) or die(mysql_error());
	$row = mysql_fetch_assoc($rs);
	$dealer_name = $row["dealer_name"];
	$phone = $row["phone"];
	$address1 = $row["address1"];
	$address2 = $row["address2"];
	$city = $row["city"];
	$state = $row["state_province"];
	$zip = $row["zip_postal"];
	$description = $row["description"];
	$dealership_id = $_GET['id'];
} 

?>

<script type="text/javascript" charset="utf-8">
	$("#dealer_edit_form").validate({
		rules: {
			dealer_name: {required: true},
			phone: {required: true, digits: true, minlength: 10, maxlength: 10},
			address1: {required: true},
			city: {required: true},
			state: {required: true, minlength: 2, maxlength: 2},
			zip: {required: true, digits: true, minlength: 5, maxlength: 5}			
		},
		
		submitHandler: function(form) {
			var options = {
				target:     '#error_msg', 
				url:        'save_dealer.php', 
				success:    function(t) { 
					// If the submit was successful, refresh the admin page.
					$("#dealer_name").focus();
					if (t == "Success") location.replace("admin.php")
				} 
			};
			
			$(form).ajaxSubmit(options);
			return false;
		}
	
	
	});
</script>


<div id="error_msg"></div>
<form action="save_dealer.php" method="post" id="dealer_edit_form">
	<dl>
	<fieldset>
		<dt><label for="dealer_name">Dealership Name</label></dt>
		<dd><input type="text" name="dealer_name" value="<?php echo $dealer_name ?>" id="dealer_name" tab_stop="1" class="required" /></dd>
		<dt><label for="phone">Primary Phone Number (no dashes)</label></dt>
		<dd><input type="text" name="phone" value="<?php echo $phone ?>" id="phone" tab_stop="2"/></dd>
		<dt><label for="address1">Address</label></dt>
		<dd><input type="text" name="address1" value="<?php echo $address1 ?>" id="address1" tab_stop="3"/></dd>
		<dt><label for="address2">Address 2</label></dt>
		<dd><input type="text" name="address2" value="<?php echo $address2 ?>" id="address2" tab_stop="4"/></dd>
		<dt><label for="city">City</label></dt>
		<dd><input type="text" name="city" value="<?php echo $city ?>" id="city" tab_stop="5"/></dd>
		<dt><label for="state">State</label></dt>
		<dd><input type="text" name="state" value="<?php echo $state ?>" id="state" tab_stop="6"/></dd>
		<dt><label for="zip">Zip</label></dt>
		<dd><input type="text" name="zip" value="<?php echo $zip ?>" id="zip" tab_stop="7"/></dd>
		<dt><label for="description">Description</label></dt><br/>
		<dd><textarea name="description" id="description" tab_stop="8" rows="8" cols="40"><?php echo $description ?></textarea></dd>
		
		<input type="hidden" name="dealership_id" value="<?php echo $dealership_id ?>" id="dealership_id" />
		<input type="submit" name="submit_dealer" id="submit_dealer" value="Save" tab_stop="9"/>
		<button type="button" id="cancel_dealer" onclick="$('#newdealer').slideUp('slow'); " tab_stop="10">Cancel</button>
	</fieldset>
    </dl>
</form>
